import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodsList: [
    //   {
    //   goods_id: 1,
    //   cat_id: 2,
    //   goods_name: '人体工学椅',
    //   goods_price: 12.00,
    //   number: 1,
    //   goods_weight: '50',
    //   goods_small_logo: 'https://img.yzcdn.cn/vant/t-thirt.jpg',
    //   is_promote: true
    // }, {
    //   goods_id: 2,
    //   cat_id: 2,
    //   goods_name: '人体工学椅',
    //   goods_price: 12.00,
    //   number: 1,
    //   goods_weight: '50',
    //   goods_small_logo: 'https://img.yzcdn.cn/vant/t-thirt.jpg',
    //   is_promote: true,
    // }
  ],
    total: 0,
    goods_id: 0,
    inputValue: '',
    allnum:0,
    ifShow: true,
    list: [],
    // 添加ID
    shuID: 1,
    // 选中ID
    judges: 0,
    // 修改数据,
    modify: {},
  },
  mutations: {
    change(state, { id, num }) {
      state.goodsList.forEach(item => {
        if (item.goods_id === id) {
          item.number = num
        }
      })
      state.total = 0
    },
    closeAll(state) {
      state.goodsList.forEach(item => {
        item.is_promote = false
      })
    },
    openAll(state) {
      state.goodsList.forEach(item => {
        item.is_promote = true
      })
    },
    submit(state) {
    },
    changeThis(state) {
      state.total = 0
    },
    getlist1(state, list) {
      state.goodsList.push(list)
      state.goodsList = Array.from(new Set(state.goodsList))
    },
    remove(state,id) {
      const i = state.goodsList.findIndex(x => x.goods_id === id)
      if (i !== -1) {
        state.goodsList.splice(i, 1)
      }
    },
    getgoodid(state, id) {
      state.goods_id = id
    },
    changeData(state, val) {
      state.inputValue = val
    },
    setIfShow(state, val) {
      state.ifShow = val
    },
    // 添加地址
    AdTheAddress(state, val) {
      if (state.list === null) {
        state.list = []
      }
      if (state.flag == false) {

        const siatlist = state.list.findIndex(item => {
          return item.id === val.id
        })
        state.list[siatlist] = val
        state.flag = true
      } else {
        state.list.push(val)
        state.shuID = (state.shuID + 1)
        state.judges = (state.shuID - 1)
        localStorage.setItem('judges', JSON.stringify(state.judges))
      }
      // 判断默认地址是否为true
      if (val.isDefault) {
        // 遍历默认地址都为false
        state.list.forEach(item => {
          item.isDefault = false
        })
        const res = state.list.find(item => {
          return val.id === item.id
        })
        res.isDefault = true
      }
      localStorage.setItem('list', JSON.stringify(state.list))
      localStorage.setItem('shuID', JSON.stringify(state.shuID))
    },
    // 赋值选中的ID
    addSelect(state, id) {
      state.judges = id
    },
    judge(state) {
      state.flag = true
    },
   
    // 修改数据
    addModify(state, val) {
      state.flag = false
      state.modify = {
        id: val.id,
        addressDetail: val.address[3],
        areaCode: val.address[5],
        city: val.address[1],
        country: "",
        county: val.address[2],
        isDefault: val.isDefault,
        name: val.name,
        postalCode: val.address[4],
        province: val.address[0],
        tel: val.tel
      }
    },
    // 
    addlistk(state) {
      const data = localStorage.getItem('list')
      state.list = JSON.parse(data)
      const list = localStorage.getItem('shuID')
      state.shuID = JSON.parse(list)
      state.judges = JSON.parse(localStorage.getItem('judges'))
      if (state.shuID === null) {
        state.shuID = 1
      }

    },
    // 
    trigger(state, val) {
      // alert(11)
      localStorage.setItem('judges', JSON.stringify(val.id))

      const data = JSON.parse(localStorage.getItem('list'))
      if (true) {
        // 遍历默认地址都为false
        data.forEach(item => {
          item.isDefault = false
        })
        const res = data.find(item => {
          return val.id === item.id
        })
        res.isDefault = true
      }
      localStorage.setItem('list', JSON.stringify(data))

    },
  },
  actions: {
  },
  modules: {
  },
  getters: {
    changeTotal(state) {
      state.goodsList.forEach(item => {
        if (item.is_promote) {
          let everyTotal = item.number * item.goods_price * 100
          state.total = state.total + everyTotal
        }
      })
      return state.total
    },
    getAllNum(state) {
      state.allnum = 0
      state.goodsList.forEach(item =>{
        if (item.is_promote) {
          state.allnum += item.number
        }
      })
      return state.allnum
    }
  }
})
